package com.m3839.sdk.common.util;


import com.m3839.sdk.common.encrypt.Md5Utils;

import java.util.Locale;

/**
 * Description:VerifyUtils
 *
 * @author 陈宝阳
 * @create 2020/8/28 16:56
 */
public class VerifyUtils {

  /**
   * 签名原始串：x1!x2!x3!x4@x5 签名最终值：md5('原始串')从第3位起，裁剪24位，如 8d7bcf355fe0720006d9147e4b47a12f
   * 裁剪后为：7bcf355fe0720006d9147e4b
   * <p>
   * 其中各个值为： x1: gameId x2: userId  未登录为0 x3: path    当前路径地址  'game/init' 类似这样 x4: time    当前时间戳
   * x5: key     key值：7AE053C3F115699A   每个版本不一致，sdk更新需重新找后端获取
   * <p>
   * 每个接口新增2个字段： time  时间戳 sign  签名值
   */

  //private static final String VERIFY_KEY = "7AE053C3F115699A"; //1.0.0.0的key
  //private static final String VERIFY_KEY = "4272CFEE4BFBE815";  //1.0.1.0的key
  //private static final String VERIFY_KEY = "24158941381AC265";  //1.0.2.0的key
  //public static final String VERIFY_KEY = "D75C39CF58303F52";   //1.0.3.0的key
  /** 1.0.4.0 的key*/
  public static final String VERIFY_KEY = "3F2617FFDBE13EA7";

  private static final String SEPARATE_SYMBOL = "!";

  private static final String SEPARATE_AT = "@";

  public static String getVerifySign(String gameId, String userId, String path, String timeStamp) {
    StringBuilder sb = new StringBuilder();
    sb.append(gameId)
        .append(SEPARATE_SYMBOL)
        .append(userId)
        .append(SEPARATE_SYMBOL)
        .append(path)
        .append(SEPARATE_SYMBOL)
        .append(timeStamp)
        .append(SEPARATE_AT)
        .append(VERIFY_KEY);
    String originSign = sb.toString();
    String sign = Md5Utils.md5(originSign);
    return sign.substring(2, 26);
  }

  /**
   * 生成加密的key，这个key是用来接口签名
   * @param version
   * @return
   */
  private static String generateKey(String version) {
    String originStr = "hykb_sdk_"+version;
    String md5Str = Md5Utils.md5(originStr);
    String subStr = md5Str.substring(8, 24);
    String result = subStr.toUpperCase(Locale.ROOT);
    return result;
  }
}
